package cn.a.bootexe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.DataOutputStream;

public class MainActivity extends Activity {

    // Content View Elements

    private Button mBtnShutdown;
    private Button mBtnReboot;
    private Button mBtnRecovery;
    private Button mBtnClose;

    // End Of Content View Elements

    private void bindViews() {

        mBtnShutdown = (Button) findViewById(R.id.btnShutdown);
        mBtnReboot = (Button) findViewById(R.id.btnReboot);
        mBtnRecovery = (Button) findViewById(R.id.btnRecovery);
        mBtnClose = (Button) findViewById(R.id.btnClose);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("xuxingcan", "onCreate222");
        setContentView(R.layout.main);
        bindViews();
        mBtnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBtnShutdown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
                intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        mBtnReboot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exec("reboot");
            }
        });
        mBtnRecovery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exec("reboot recovery");
            }
        });
    }

    public static void exec(String paramString) {
        try {
            Process localProcess = Runtime.getRuntime().exec("su");
            DataOutputStream localDataOutputStream = new DataOutputStream(localProcess.getOutputStream());
            localDataOutputStream.writeBytes(paramString + "\n");
            localDataOutputStream.writeBytes("exit\n");
            localDataOutputStream.flush();
            localDataOutputStream.close();
            localProcess.waitFor();
            localProcess.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
